package com.blast.cn.persenter.more

import android.app.Service
import android.media.AudioManager
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.widget.AppCompatSeekBar
import com.wxl.common.viewmodel.AbsPresenter

class VoiceSeekPresenter : AbsPresenter() {

    private var mAudioManager: AudioManager? = null
    private var mVoiceMaxNum = 0
    private var mCurrentVoiceNum = 0

    override fun onCreated(vararg args: Any) {
        val seek = args[0] as AppCompatSeekBar
        val seekText = args[1] as TextView

        mAudioManager = seek.context.getSystemService(Service.AUDIO_SERVICE) as AudioManager
        mVoiceMaxNum = mAudioManager!!.getStreamMaxVolume(AudioManager.STREAM_SYSTEM)
        mCurrentVoiceNum = mAudioManager!!.getStreamVolume(AudioManager.STREAM_SYSTEM)

        seek.max = mVoiceMaxNum
        seek.progress = mCurrentVoiceNum
        seekText.text = (mCurrentVoiceNum*100/mVoiceMaxNum).toString() +"%"

        seek.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                mCurrentVoiceNum = progress
                try {
                    mAudioManager!!.setStreamVolume(AudioManager.STREAM_SYSTEM, progress, AudioManager.FLAG_VIBRATE)
                    seekText.setText((mCurrentVoiceNum * 100 / mVoiceMaxNum).toString() + "%")
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }
        })
    }
}